/** TODO: Finish this problem. */
#include<iostream>
using namespace std;
struct pi { int x; int y;
  pi(){};
  pi(int x, int y) {
    this->x = x; this->y = y;
  }};
const int maxn = 22;
pi S, G;
int w, h, grid[maxn][maxn];
int cnt = 0;
int ans = 9999;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
// const string dc = "DLUR";
void dfs(pi p, int di, int dep) {
  if(dep>10)return;
  int x=p.x, y=p.y;
  int bakx, baky; // backup
  while(grid[x][y]==0) {
    x+=dx[di], y+=dy[di];
    if(x<0 || y<0 || h<=x || w<=y)return;
  }
  if(grid[x][y]==3) {
    ans=min(ans, dep);return;
  }
  else {
    bakx=x, baky=y;
    grid[x][y]=0;
    x-=dx[di], y-=dy[di];
  }
  for(int i=0; i<4; ++i) {
    if(grid[x+dx[i]][y+dy[i]]!=1)
      dfs(pi(x,y), i, dep+1);
  }
  grid[bakx][baky] = 1; // recover(backtrack)
}
bool valid(int x, int y) {
  return !(x<0 || y<0 || h<=x || w<=y);
}
void solve() {
  for(int i=0; i<4; ++i) {
    if(valid(S.x+dx[i], S.y+dx[i]) && grid[S.x+dx[i]][S.y+dy[i]]!=1) {
      dfs(S, i, 1);
    }
  }
  if(ans!=9999)cout<<ans<<'\n';
  else cout<<"-1\n";
}

int main() {
  freopen("tc/in4.txt", "r", stdin);
  while(cin>>w>>h) {
    cnt=0;
    ans=9999;
    S=G=pi(-1,-1);
    if(w==0&&h==0)return 0;
    for(int i=0; i<h; ++i) {
      for(int j=0; j<w; ++j) {
        cin>>grid[i][j];
      }
    }
    for(int i=0; i<h; ++i) {
      for(int j=0; j<w; ++j) {
        if(S.x==-1 && grid[i][j]==2)S=pi(i,j), grid[i][j]=0;
        if(G.x==-1 && grid[i][j]==3)G=pi(i,j);
      }
    }
    solve();
  }
  return 0;
}
